//package com.xinhe.web.exception;
//
//import com.alibaba.fastjson.JSON;
//import com.xinhe.web.common.BusinessException;
//import com.xinhe.web.common.Result;
//import lombok.extern.slf4j.Slf4j;
//import org.codehaus.jettison.json.JSONObject;
//import org.springframework.web.servlet.HandlerExceptionResolver;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//@Slf4j
//public class HandlerExceptionResolverBean implements HandlerExceptionResolver {
//
//    @Override
//    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
//        Result result = null;
//        if (e instanceof BusinessException) {
//            log.info("自定义异常:" + e.getMessage());
//            BusinessException be = (BusinessException)e;
//            result = Result.failed(be.getErrorCode(), be.getMessage());
//        } else {
//            log.error("未捕获的异常:" + e.getMessage(), e);
//            result = Result.failed(7777, e.getMessage());
//        }
//        response.setHeader("Content-type", "application/json;charset=UTF-8");
//        try (ServletOutputStream outputStream = response.getOutputStream()) {
//            JSONObject reJsonObj = (JSONObject) JSON.toJSON(result);
//            outputStream.write(reJsonObj.toString().getBytes(StandardCharsets.UTF_8));
//        } catch (IOException e1) {
//            log.error(e1.getMessage(), e1);
//        }
//        return null;
//    }
//}
